<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <el-select v-model="pageForm.searchType" placeholder="请选择日志类型"
                       size="medium"
            >
                <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                >
                </el-option>
            </el-select>
        </div>
        <div class="btn-group tool-button mt5">
            <el-date-picker
                    v-model="searchDate"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    align="right"
                    size="medium"
                    :default-time="['00:00:00', '23:59:59']"
                    value-format="yyyy-MM-dd HH:mm:ss"
            >
            </el-date-picker>
        </div>
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch" size="medium"></el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                @sort-change='pageOrder'
                size="small"
                header-align="center"
                style="width: 100%"
                :default-sort = "{prop: 'createdat', order: 'descending'}"
        >
            <el-table-column
                    sortable
                    prop="username"
                    label="操作人"
                    header-align="center" align="center">
                <template slot-scope="scope">
                    {{scope.row.username+'/'+scope.row.loginname}}
                </template>
            </el-table-column>
            <el-table-column
                    sortable
                    prop="createdat"
                    label="操作时间"
                    width="120"
                    header-align="center" align="center">
                <template slot-scope="scope">
                    {{formatAt(scope.row.createdat)}}
                </template>
            </el-table-column>
            <el-table-column
                    prop="type"
                    label="日志类型"
                    width="120"
                    header-align="center" align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.type=='platform'">
                        平台日志
                    </span>
                    <span v-if="scope.row.type=='info'">
                        登录日志
                    </span>
                    <span v-if="scope.row.type=='api'">
                        接口日志
                    </span>
                </template>
            </el-table-column>

            <el-table-column
                    prop="tag"
                    width="120"
                    label="执行动作"
                    header-align="center" align="center">
            </el-table-column>
            <el-table-column
                    prop="msg"
                    label="日志内容"
                    header-align="left"
            >
            </el-table-column>

            <el-table-column
                    prop="src"
                    label="执行类"
                    header-align="left"
                    :show-overflow-tooltip="true"
            >
            </el-table-column>
            <el-table-column
                    prop="ip"
                    label="IP"
                    header-align="left"
                    :show-overflow-tooltip="true"
            >
            </el-table-column>
        </el-table>
    </el-row>
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>
</div>

<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                options: [{
                    value: '',
                    label: '全部类型'
                }, {
                    value: 'platform',
                    label: '平台日志'
                }, {
                    value: 'info',
                    label: '登录日志'
                }, {
                    value: 'api',
                    label: '接口日志'
                }],
                pickerOptions: {
                    disabledDate: function (time) {
                        var threeMonths = moment().add(-3, 'months').valueOf();
                        return time.getTime() > Date.now() || time.getTime() < threeMonths;
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick:function(picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick:function(picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick:function(picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                tableData: [],
                pageForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    pageOrderName: "createdat",
                    pageOrderBy: "descending",
                    searchDate: "",
                    searchType: ""
                },
                searchDate: []
            }
        },
        methods: {
            formatAt: function (val) {
                if (val > 0)
                    return moment(val).format("YYYY-MM-DD HH:mm");
                return "";
            },
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                }else {
                    self.pageForm.searchDate="";
                }
                $.post(base + "/platform/sys/log/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code == 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
        },
        created: function () {
            this.pageData();
        }
    });
</script>
<!--#
}
#-->